package com.xiaol.ray.atomikosdemo.controller;

import com.xiaol.ray.atomikosdemo.entity.BookDO;
import com.xiaol.ray.atomikosdemo.entity.BookVo;
import com.xiaol.ray.atomikosdemo.service.BookService;
import com.xiaol.ray.atomikosdemo.service.impl.BookServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * User: Ray.Shaw <p />
 * Time: 2019/10/28 17:55  <p />
 * Version: V1.0  <p />
 * Description:  <p />
 */
@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping
    public List<BookDO> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return bookService.list(page, size);
    }

    @GetMapping("/{id}")
    public BookDO get(@PathVariable Long id) {
        return bookService.get(id);
    }

    @PostMapping
    public BookDO save(@RequestBody BookVo book) {
        return bookService.save(book, book.getUser());
    }

    @PutMapping
    public BookDO update(@RequestBody BookVo book) {
        return ((BookServiceImpl) bookService).updateBook(book, book.getUser());
    }

    @DeleteMapping("/{id}")
    public int delete(@PathVariable Long id) {
        return ((BookServiceImpl) bookService).delete(id);
    }
}
